web/src/pages/search/[...searchTerm].astro
205 lines · 4799 bytes
1---
2import Fuse from 'fuse.js';
3
4import Layout from '@layouts/Layout.astro';
5import { fetchData, slugify } from '@utils/fetch-data';
6import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
7import type { SearchItem } from '@utils/do-searchy-searchy';
8import Search from '@components/things/Search.svelte';
9import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
10import FontAwesome from '@components/form/FontAwesome.svelte';
11
12export const prerender = false;
13
14let fuse: Fuse<SearchItem>;
15
16const categories = (await fetchData())?.categories;
17
18const items = prepareSearchItems(categories);
19
20fuse = new Fuse(items, searchOptions);
21
22const searchTerm = Astro.params.searchTerm;
23
24const searchResults = fuse
25 .search(searchTerm || '')
26 .map((result) => result.item);
27
28const services = searchResults.filter((result) => result.type === 'Service');
29
30interface GroupedSection {
31 sectionName: string;
32 items: SearchItem[];
33}
34
35interface GroupedCategory {
36 categoryName: string;
37 sections: Record<string, GroupedSection>;
38}
39
40const putResultsIntoGroups = () => {
41 const grouped: Record<string, GroupedCategory> = {};
42
43 for (const item of services) {
44 const categoryName = item.category;
45 const sectionName = item.sectionName || '';
46
47 if (!grouped[categoryName]) {
48 grouped[categoryName] = { categoryName, sections: {} };
49 }
50
51 if (!grouped[categoryName].sections[sectionName]) {
52 grouped[categoryName].sections[sectionName] = { sectionName, items: [] };
53 }
54
55 grouped[categoryName].sections[sectionName].items.push(item);
56 }
57
58 return Object.values(grouped).map((category) => ({
59 categoryName: category.categoryName,
60 sections: Object.values(category.sections),
61 }));
62};
63
64const beer = putResultsIntoGroups();
65---
66
67<Layout title="Search | Awesome Privacy">
68 <section>
69 <h1>Search</h1>
70 <Search client:visible data={categories} previousSearch={searchTerm} />
71 </section>
72 <SmartSuggestions
73 client:visible
74 searchTerm={searchTerm || ''}
75 categories={categories}
76 />
77 <section class="result-count">
78 <h3>Deep Search</h3>
79 <p>
80 Showing {services.length} results for "{searchTerm}" sorted by relevence
81 </p>
82 </section>
83 <div class="results">
84 {
85 beer.map((category) => (
86 <div class="category">
87 <a class="category-title" href={`/${slugify(category.categoryName)}`}>
88 <h3>{category.categoryName}</h3>
89 </a>
90 <span class="section-icon">
91 <FontAwesome iconName={slugify(category.categoryName)} />
92 </span>
93 <ul class="section-list">
94 {category.sections.map((section) => (
95 <li class="section-item">
96 <h4>{section.sectionName}</h4>
97 <ul class="service-list">
98 {section.items.map((item) => (
99 <li>
100 <a href={item.url}>{item.name}</a>
101 </li>
102 ))}
103 </ul>
104 </li>
105 ))}
106 </ul>
107 </div>
108 ))
109 }
110 </div>
111</Layout>
112
113<style lang="scss">
114 section {
115 display: flex;
116 flex-direction: column;
117 margin: var(--space-lg) auto;
118 padding: var(--space-md);
119 width: calc(100% - 2rem);
120 max-width: var(--width-container);
121
122 h1 {
123 font-size: var(--text-4xl);
124 color: var(--accent-3);
125 font-family: var(--font-subtitle);
126 text-align: center;
127 margin: var(--space-md) 0;
128 }
129 }
130
131 .result-count {
132 padding: 0;
133 width: calc(100% - 2rem);
134 margin: var(--space-md) auto 0 auto;
135 h3 {
136 margin: 0;
137 color: var(--accent-3);
138 font-size: var(--text-xl);
139 }
140 p {
141 margin: 0;
142 opacity: var(--opacity-muted);
143 }
144 }
145
146 .results {
147 margin: 0 auto var(--space-lg) auto;
148 padding: var(--space-md);
149 width: calc(100% - 2rem);
150 max-width: var(--width-container);
151 columns: 6 300px;
152 column-gap: var(--space-md);
153 .category {
154 padding: var(--space-md);
155 box-shadow: var(--shadow-md);
156 background: var(--accent-fg);
157 border: var(--border-heavy);
158 border-radius: var(--curve-sm);
159 display: inline-flex;
160 flex-direction: column;
161 width: 85%;
162 margin: var(--space-md);
163 position: relative;
164 .category-title {
165 text-decoration: none;
166 color: var(--foreground);
167 z-index: 2;
168 position: relative;
169 h3 {
170 font-family: var(--font-subtitle);
171 font-weight: bold;
172 margin: 0;
173 font-size: var(--text-2xl);
174 }
175 }
176 .section-icon {
177 position: absolute;
178 right: var(--space-sm);
179 top: var(--space-sm);
180 width: fit-content;
181 :global(svg) {
182 width: var(--text-3xl);
183 height: var(--text-3xl);
184 opacity: var(--opacity-dim);
185 text-shadow: 3px 3px 0 black;
186 color: var(--accent-3);
187 transition: var(--transition-normal);
188 }
189 }
190 .section-list {
191 list-style: none;
192 padding: 0;
193 margin: 0;
194 h4 {
195 margin: var(--space-md) 0 0 0;
196 }
197 .service-list {
198 list-style: none;
199 padding: 0;
200 margin: 0;
201 }
202 }
203 }
204 }
205</style>